package tpase.StorageServer.DO;

import javafx.beans.property.SimpleStringProperty;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

public class FileInfo {
    private String fid;
    private String fileName;
    private String path;
    private Long size;
    private String type;
    private Timestamp createTime;
    private String uid;


    //public SimpleIntegerProperty idString;
    public SimpleStringProperty nameString;
    public SimpleStringProperty typeString;
    public SimpleStringProperty sizeString;
    public SimpleStringProperty timeString;

    public FileInfo() {
    }

    @Override
    public String toString() {
        return "FileInfo{" +
                "fid='" + fid + '\'' +
                ", fileName='" + fileName + '\'' +
                ", path='" + path + '\'' +
                ", size=" + size +
                ", type='" + type + '\'' +
                ", createTime=" + createTime +
                ", uid='" + uid + '\'' +
                '}';
    }

    public String getNameString() {
        return nameString.get();
    }

    public SimpleStringProperty nameStringProperty() {
        return nameString;
    }

    public void setNameString(String nameString) {
        this.nameString.set(nameString);
    }

    public String getTypeString() {
        return typeString.get();
    }

    public SimpleStringProperty typeStringProperty() {
        return typeString;
    }

    public void setTypeString(String typeString) {
        this.typeString.set(typeString);
    }

    public String getSizeString() {
        return sizeString.get();
    }

    public SimpleStringProperty sizeStringProperty() {
        return sizeString;
    }

    public void setSizeString(String sizeString) {
        this.sizeString.set(sizeString);
    }

    public String getTimeString() {
        return timeString.get();
    }

    public SimpleStringProperty timeStringProperty() {
        return timeString;
    }

    public void setTimeString(String timeString) {
        this.timeString.set(timeString);
    }

    public FileInfo(String fid, String fileName, String path, Long size, String type, Timestamp createTime, String uid) {
        this.fid = fid;
        this.fileName = fileName;
        this.path = path;
        this.size = size;
        this.type = type;
        this.createTime = createTime;
        this.uid = uid;


        this.typeString = new SimpleStringProperty(type);
        this.sizeString = sizeFormat(size);
        this.timeString = new SimpleStringProperty(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(createTime));
        this.nameString = new SimpleStringProperty(fileName);
    }

    private SimpleStringProperty sizeFormat(Long size) {
        if (size < 1024) {
            return new SimpleStringProperty(String.format("%.2f", 1.0 * size) + " B");
        }
        if (size < 1024 * 1024) {
            return new SimpleStringProperty(String.format("%.2f", 1.0 * size / 1024) + " KB");
        }
        return new SimpleStringProperty(String.format("%.2f", 1.0 * size / 1024 / 1024) + " MB");
    }

    public String getFid() {
        return fid;
    }

    public void setFid(String fid) {
        this.fid = fid;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
        this.nameString = new SimpleStringProperty(fileName);
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public Long getSize() {
        return size;
    }

    public void setSize(Long size) {
        this.size = size;
        this.sizeString = sizeFormat(size);
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
        this.typeString = new SimpleStringProperty(type);
    }

    public Timestamp getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Timestamp createTime) {
        this.createTime = createTime;
        this.timeString = new SimpleStringProperty(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(createTime));
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

}
